<?php

namespace app\admin\controller\system;

use app\common\controller\Admin;
use app\common\traits\ControllerCurd;
use think\Request;
use app\common\annotation\NodeAnnotation;
/**
 * @package app\admin\controller\system
 * @NodeAnnotation(
 *     title="通告文章板块",
 *     is_menu=0,
 *     is_view=0
 * )
 */
class Notice extends  Admin
{
    use ControllerCurd;
    public $model = '';
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\system\Notice();
    }

    /**
     * @NodeAnnotation(
     *     title="通告管理",
     *     parent="admin/system.notice",
     *     MenuRoot="admin/system.setting"
     * )
     */
    public function index(Request $request)
    {
        $keywords  = $request->param("keywords");

        $where = [];

        if($keywords) array_push($where,['post_title',"like","%$keywords%"]);

        $postList = $this->model->where($where)
            ->order("update_time","desc")
            ->paginate();

        $this->assign([
            "postList"=> $postList,
            "search"  => [
                "keywords" => $keywords
            ]
        ]);
        return view();
    }

    /**
     * @NodeAnnotation(
     *     title="添加通告",
     *     parent="admin/system.notice",
     *     MenuRoot="admin/system.setting",
     *     is_menu = 0
     * )
     */
    public function addpost(Request $request)
    {

        $this->add(url("admin/system.notice/index"));
        return view();
    }

    /**
     * @NodeAnnotation(
     *     title="编辑通告",
     *     parent="admin/system.notice",
     *     MenuRoot="admin/system.setting",
     *     is_menu = 0
     * )
     */
    public function editpost(Request $request,$id=0)
    {
        $post = $this->model->find($id);

        $input = $request->post();
        if(empty($input['post_status'])) $input['post_status'] = 0;
        if(empty($input['recommended'])) $input['recommended'] = 0;
        if(empty($input['is_top'])) $input['is_top'] = 0;

        $this->edit(url("admin/system.notice/index"),function(){},$input);

        $this->assign([
            "post"         => $post,
        ]);
        return view();
    }

    /**
     * @NodeAnnotation(
     *     title="删除通告",
     *     parent="admin/system.notice",
     *     MenuRoot="admin/system.setting",
     *     is_menu = 0,
     *     is_view = 0
     * )
     */
    public function delpost()
    {
        $this->del();
    }
}